package com.cuhk.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cuhk.gulimall.ware.vo.MergeVo;
import com.cuhk.gulimall.ware.vo.PurchaseFinishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cuhk.gulimall.ware.entity.PurchaseEntity;
import com.cuhk.gulimall.ware.service.PurchaseService;
import com.cuhk.common.utils.PageUtils;
import com.cuhk.common.utils.R;

import static com.cuhk.common.constant.ControllerConstant.ExecuteOutcomeCode.SUCCESS_DEAL;


/**
 * 采购信息
 *
 * @author chenyujing
 * @email torchsam@aliyun.com
 * @date 2024-06-10 14:49:06
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    // /ware/purchase/unreceived/list
    @RequestMapping("/unreceive/list")
    public R unreceivedList(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPageUnReceived(params);
        return R.ok().put("page", page);
    }

    // /ware/purchase/received
    // 领取采购单接口
    @PostMapping("/received")
    public R receivedList(@RequestBody List<Long> ids){
        purchaseService.received(ids);
        return R.ok();
    }

    // 合并采购需求
    // 作为一个采购单
    // /ware/purchase/merge
    @PostMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo){
        Map<String, Object> result = purchaseService.mergePurchase(mergeVo);
        if(result.get("code") == SUCCESS_DEAL){
            return R.ok().put("msg", result.get("msg"));
        }
        return R.error().put("msg", result.get("msg"));
    }

    @PostMapping("/finish")
    public R purchaseFinished(@RequestBody PurchaseFinishVo purchaseFinishVo) {
        Map<String, Object> result = purchaseService.finishPurchase(purchaseFinishVo);
        if(result.get("code") == SUCCESS_DEAL){
            return R.ok().put("msg", result.get("msg"));
        }
        return R.error().put("msg", result.get("msg"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);
        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
